package com.example.framework;

import com.opera.core.systems.OperaDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class HelperManager {
    private WebDriver driver;
    private NavigationHelper navigationHelper;
    private GroupHelper groupHelper;
    private ContactHelper contactHelper;
    private Properties properties;
    private HibernateHelper hibernateHelper;

    public HelperManager(Properties properties) {
        this.properties = properties;
    }

    public void stopDriver(){
        driver.quit();
    }

    public NavigationHelper getNavigationHelper(){
        if (navigationHelper == null){
            navigationHelper = new NavigationHelper(this);
        }
        return navigationHelper;
    }

    public GroupHelper getGroupHelper(){
        if (groupHelper == null){
            groupHelper = new GroupHelper(this);
        }
        return groupHelper;
    }

    public ContactHelper getContactHelper(){
        if (contactHelper == null){
            contactHelper = new ContactHelper(this);
        }
        return contactHelper;
    }

    public WebDriver getDriver(){
        String browser = properties.getProperty("browser");
        if (driver == null){
            if(browser.equals("firefox")){
                driver = new FirefoxDriver();
            }else if (browser.equals("iexplore")){
                driver = new InternetExplorerDriver();
            }else if (browser.equals("chrome")){
                driver = new ChromeDriver();
            }else if (browser.equals("opera")){
                driver = new OperaDriver();
            }else {
                throw new Error("Unsupported browser: " + browser);
            }
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.get(properties.getProperty("baseUrl"));
        }
        return driver;
    }

    public HibernateHelper getHibernateHelper(){
        if (hibernateHelper == null){
            hibernateHelper = new HibernateHelper(this);
        }
        return hibernateHelper;
    }
}
